package leetcode;

import java.util.Stack;

/**
 * 双栈实现队列
 */
public class MyQueue {

    private Stack<Integer> stack1 = new Stack<>();

    private Stack<Integer> stack2 = new Stack<>(); // 辅助栈

    public MyQueue() {}

    public void push(int x) {
        if (stack1.isEmpty()) {
            stack1.push(x);
        } else {
            while (!stack1.isEmpty()) {
                stack2.push(stack1.pop());
            }
            stack1.push(x);
            while (!stack2.isEmpty()) {
                stack1.push(stack2.pop());
            }
        }
    }

    public int pop() {
        return stack1.pop();
    }

    public int peek() {
        return stack1.peek();
    }

    public boolean empty() {
        if (stack1.isEmpty()) {
            return true;
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("front [");
        for (int i = 0; i < stack1.size(); i++) {
            stringBuilder.append(stack1.get(i));
            if (i != stack1.size() - 1) {
                stringBuilder.append(", ");
            }
        }
        stringBuilder.append("] tail");
        return stringBuilder.toString();
    }
}

class Test {
    public static void main(String[] args) {
        MyQueue myQueue = new MyQueue();
        for (int i = 1; i <= 5; i++) {
            myQueue.push(i);
        }
        System.out.println(myQueue);
        myQueue.pop();
        System.out.println(myQueue);
        System.out.println("peek:" + myQueue.peek());
    }
}
